package com.ruoyi.system.service.impl;

import com.ruoyi.system.service.IVegetableMarketService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 蔬菜市场数据服务实现
 */
@Service
public class VegetableMarketServiceImpl implements IVegetableMarketService {

    @Autowired
    private SqlSession sqlSession;

    @Override
    public Map<String, Object> getVegetableMarketData() {
        Map<String, Object> result = new HashMap<>();
        
        // 指标卡数据
        List<Map<String, Object>> indicatorCards = new ArrayList<>();
        
        Map<String, Object> card1 = new HashMap<>();
        card1.put("title", "这里是标题1");
        card1.put("value", "6890.69");
        card1.put("compare1", "大于");
        card1.put("compare2", "大于");
        indicatorCards.add(card1);
        
        Map<String, Object> card2 = new HashMap<>();
        card2.put("title", "这里是标题2");
        card2.put("value", "6090.99");
        card2.put("compare1", "大于");
        card2.put("compare2", "大于");
        indicatorCards.add(card2);
        
        Map<String, Object> card3 = new HashMap<>();
        card3.put("title", "这里是标题3");
        card3.put("value", "2890.39");
        card3.put("compare1", "大于");
        card3.put("compare2", "大于");
        indicatorCards.add(card3);
        
        Map<String, Object> card4 = new HashMap<>();
        card4.put("title", "这里是标题4");
        card4.put("value", "7590.15");
        card4.put("compare1", "大于");
        card4.put("compare2", "大于");
        indicatorCards.add(card4);
        
        result.put("indicatorCards", indicatorCards);
        
        // 饼图数据
        List<Map<String, Object>> pieChartData = new ArrayList<>();
        
        Map<String, Object> pie1 = new HashMap<>();
        pie1.put("value", 30);
        pie1.put("name", "西红柿");
        pieChartData.add(pie1);
        
        Map<String, Object> pie2 = new HashMap<>();
        pie2.put("value", 25);
        pie2.put("name", "黄瓜");
        pieChartData.add(pie2);
        
        Map<String, Object> pie3 = new HashMap<>();
        pie3.put("value", 20);
        pie3.put("name", "白菜");
        pieChartData.add(pie3);
        
        Map<String, Object> pie4 = new HashMap<>();
        pie4.put("value", 15);
        pie4.put("name", "茄子");
        pieChartData.add(pie4);
        
        Map<String, Object> pie5 = new HashMap<>();
        pie5.put("value", 10);
        pie5.put("name", "豆角");
        pieChartData.add(pie5);
        
        result.put("pieChartData", pieChartData);
        
        // 统计数据
        Map<String, Object> stats = new HashMap<>();
        stats.put("today", 23);
        stats.put("month", 107);
        stats.put("year", 107);
        stats.put("today2", 23);
        stats.put("month2", 23);
        stats.put("year2", 23);
        
        result.put("stats", stats);
        
        return result;
    }

    @Override
    public Map<String, Object> getSalesRankData() {
        Map<String, Object> result = new HashMap<>();
        
        // 从数据库获取菜品价格排行数据
        List<Map<String, Object>> priceRankList = sqlSession.selectList("com.ruoyi.system.mapper.DishesMapper.selectDishesOrderByPrice");
        
        // 处理排行数据
        List<Map<String, Object>> salesRank = new ArrayList<>();
        for (int i = 0; i < priceRankList.size(); i++) {
            Map<String, Object> dish = priceRankList.get(i);
            Map<String, Object> rankItem = new HashMap<>();
            rankItem.put("rank", i + 1);
            rankItem.put("name", dish.get("name"));
            rankItem.put("price", dish.get("price"));
            salesRank.add(rankItem);
        }
        
        result.put("salesRank", salesRank);
        return result;
    }

    @Override
    public Map<String, Object> getSalesTrendData() {
        Map<String, Object> result = new HashMap<>();
        
        // 柱状图数据
        Map<String, Object> barChartData = new HashMap<>();
        List<String> xData = new ArrayList<>();
        xData.add("1月");
        xData.add("2月");
        xData.add("3月");
        xData.add("4月");
        xData.add("5月");
        xData.add("6月");
        xData.add("7月");
        xData.add("8月");
        xData.add("9月");
        xData.add("10月");
        xData.add("11月");
        xData.add("12月");
        
        List<Integer> yData = new ArrayList<>();
        yData.add(220);
        yData.add(182);
        yData.add(291);
        yData.add(234);
        yData.add(290);
        yData.add(330);
        yData.add(410);
        yData.add(123);
        yData.add(156);
        yData.add(180);
        yData.add(210);
        yData.add(199);
        
        barChartData.put("xData", xData);
        barChartData.put("yData", yData);
        
        result.put("barChartData", barChartData);
        
        // 折线图数据
        Map<String, Object> lineChartData = new HashMap<>();
        
        List<String> lineXData = new ArrayList<>();
        lineXData.add("00:00");
        lineXData.add("02:00");
        lineXData.add("04:00");
        lineXData.add("06:00");
        lineXData.add("08:00");
        lineXData.add("10:00");
        lineXData.add("12:00");
        
        List<Map<String, Object>> series = new ArrayList<>();
        
        Map<String, Object> series1 = new HashMap<>();
        series1.put("name", "去年");
        List<Integer> data1 = new ArrayList<>();
        data1.add(350);
        data1.add(250);
        data1.add(300);
        data1.add(380);
        data1.add(650);
        data1.add(450);
        data1.add(350);
        series1.put("data", data1);
        series.add(series1);
        
        Map<String, Object> series2 = new HashMap<>();
        series2.put("name", "今年");
        List<Integer> data2 = new ArrayList<>();
        data2.add(220);
        data2.add(150);
        data2.add(240);
        data2.add(280);
        data2.add(380);
        data2.add(250);
        data2.add(300);
        series2.put("data", data2);
        series.add(series2);
        
        lineChartData.put("xData", lineXData);
        lineChartData.put("series", series);
        
        result.put("lineChartData", lineChartData);
        
        return result;
    }
} 